package com.javasm.controller;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.AdminMenu;
import com.javasm.entity.AdminUser;
import com.javasm.entity.PageInfo;
import com.javasm.entity.ReturnCode;
import com.javasm.service.impl.LoginServiceImpl;
import com.javasm.service.impl.MenuServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WangJie
 * @version 1.0
 * @Description:
 * @date: 2020/7/15 20:21
 * @since JDK 1.8
 */
@WebServlet("/showMenu")
public class MenuServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        List<AdminMenu> adminMenus = null;
        List<AdminUser> adminUsers = null;
        PageInfo pageInfo = new PageInfo();
        Map<String, Object> returnData = new HashMap<>();
        PrintWriter pw = resp.getWriter();

        if ("getMenu".equals(method)) {
            adminMenus = getMenu(req, resp);
        } else if ("getLimitAccount".equals(method)) {
            adminMenus = getMenu(req, resp);
            pageInfo = getLimitAccount(req, resp);
            returnData.put("pageInfo", pageInfo);
        } else if ("getMenuHead".equals(method)) {
            adminMenus = getMenu(req, resp);
            List<AdminMenu> menuHead = getMenuHead(req, resp);
            returnData.put("menuHead", menuHead);
        } else if ("getChildMenu".equals(method)) {
            List<AdminMenu> childMenu = getChildMenu(req, resp);
            returnData.put("childMenu", childMenu);
            pw.print(JSON.toJSONString(returnData));
            pw.flush();
            pw.close();
            return;
        } else if ("getAllMenu".equals(method)) {  // 递归遍历，得到所有级联菜单，用于权限控制数据的回显
            List<Map<String, Object>> allMenu = new MenuServiceImpl().selectAllMenu();
            pw.print(JSON.toJSONString(allMenu));
            pw.flush();
            pw.close();
            return;
        }

        AdminUser loginAccount = (AdminUser) req.getSession().getAttribute("loginAccount");
        // 判断查询条件
        if (adminMenus == null && pageInfo.getAdminUserList() == null) {
            returnData.put("returnCode", ReturnCode.ERROR.getCode());
            returnData.put("returnMsg", ReturnCode.ERROR.getMsg());
        } else {
            returnData.put("returnCode", ReturnCode.SUCCESS.getCode());
            returnData.put("returnData", adminMenus);
            returnData.put("returnAccount", loginAccount);

        }
        pw.print(JSON.toJSONString(returnData));
        pw.flush();
        pw.close();
    }

    //通过pid查询子菜单
    private List<AdminMenu> getChildMenu(HttpServletRequest req, HttpServletResponse resp) {
        int pid = Integer.parseInt(req.getParameter("pid"));
        return new MenuServiceImpl().selectMenuByPid(pid);
    }

    // 得到父id为0的菜单信息
    private List<AdminMenu> getMenuHead(HttpServletRequest req, HttpServletResponse resp) {
        return new MenuServiceImpl().selectMenuHead();

    }


    // 分页查询用户信息
    private PageInfo getLimitAccount(HttpServletRequest req, HttpServletResponse resp) {
        int page = Integer.parseInt(req.getParameter("page"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        List<AdminUser> adminUsers = new LoginServiceImpl().selectAdminByPage(page, pageSize);
        int totalCount = new LoginServiceImpl().selectTotalCount();
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPage(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotalCount(totalCount);
        pageInfo.setAdminUserList(adminUsers);
        return pageInfo;
    }

    // 根据登录的账户信息侧边栏展示可操作的菜单列表
    private List<AdminMenu> getMenu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AdminUser adminUser = (AdminUser) req.getSession().getAttribute("loginAccount");
        return new MenuServiceImpl().selectList(adminUser.getMenu_id());
    }
}
